<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="店铺ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <j-search-select-tag v-model="model.shopId" dict="ht_shop,name,id" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>

          <a-col>
            <address-form ref="addressForm" :showCaptcha="false" :showCheckbox="false"> </address-form>
          </a-col>

          <!-- <a-col :span="24">
            <a-form-model-item label="省级ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="provinceId">
              <j-dict-select-tag
                type="list"
                v-model="model.provinceId"
                dictCode="ht_area,name,id"
                placeholder="请选择省级ID"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="市级ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <j-dict-select-tag
                type="list"
                v-model="model.cityId"
                dictCode="ht_area,name,id"
                placeholder="请选择市级ID"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="区ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="districtId">
              <j-dict-select-tag
                type="list"
                v-model="model.districtId"
                dictCode="ht_area,name,id"
                placeholder="请选择区ID"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="街道地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="streetAddress">
              <a-textarea v-model="model.streetAddress" rows="4" placeholder="请输入街道地址" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="楼/门牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suiteAddress">
              <a-textarea v-model="model.suiteAddress" rows="4" placeholder="请输入楼/门牌号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPerson">
              <a-input v-model="model.contactPerson" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phoneNumber">
              <a-input v-model="model.phoneNumber" placeholder="请输入手机电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="座机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
              <a-input v-model="model.telephone" placeholder="请输入座机电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="传真" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fax">
              <a-input v-model="model.fax" placeholder="请输入传真"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入邮箱"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipcode">
              <a-input-number v-model="model.zipcode" placeholder="请输入邮编" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input-number v-model="model.longitude" placeholder="请输入经度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input-number v-model="model.latitude" placeholder="请输入纬度" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item
              label="是否禁用(0-启用 1-禁用)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isLocked"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isLocked"
                dictCode="is_locked"
                placeholder="请选择是否禁用(0-启用 1-禁用)"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import AddressForm from '@/components/htxincailiao/address/AddressFrom.vue'

export default {
  name: 'HTShopAddressForm',
  components: { AddressForm },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        isLocked: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        shopId: [{ required: true, message: '请输入店铺ID!' }],
        // provinceId: [
        //    { required: true, message: '请输入省级ID!'},
        // ],
        // cityId: [
        //    { required: true, message: '请输入市级ID!'},
        // ],
        // districtId: [
        //    { required: true, message: '请输入区ID!'},
        // ],
        // streetAddress: [
        //    { required: true, message: '请输入街道地址!'},
        // ],
        // contactPerson: [
        //    { required: true, message: '请输入联系人!'},
        // ],
        // phoneNumber: [
        //    { required: true, message: '请输入手机电话!'},
        //    { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
        // ],
        // email: [
        //    { required: false},
        //    { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
        // ],
        // zipcode: [
        //    { required: false},
        //    { pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮政编码!'},
        // ],
        // longitude: [
        //    { required: false},
        //    { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
        // ],
        // latitude: [
        //    { required: false},
        //    { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
        // ],
        isLocked: [{ required: true, message: '请输入是否禁用(0-启用 1-禁用)!' }]
      },
      url: {
        add: '/address/hTShopAddress/add',
        edit: '/address/hTShopAddress/edit',
        queryById: '/address/hTShopAddress/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      that.$refs.addressForm.submitForm().then(model => {
        if (model) {
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true
              let httpurl = ''
              let method = ''
              if (!this.model.id) {
                httpurl += this.url.add
                method = 'post'
              } else {
                httpurl += this.url.edit
                method = 'put'
              }

              console.log('\n\n model = ' + JSON.stringify(model) + '\n\n')

              const parameters = {
                ...that.model,
                ...model
              }

              httpAction(httpurl, parameters, method)
                .then(res => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })
        }
      })
    }
  }
}
</script>
